<template>
  <view>
    <product-box
      :productList="productList"
      :isFapin="true"
      v-if="!rule_show"
      @choose_bargin="choose_bargin"
    ></product-box>
    <view class="add_rule_container" v-if="rule_show">
          <van-field
        :value="bargin_ruleForm.cut_item_title"
        placeholder="请输入自定义名称"
        :border="false"
        @change="change_cut_title"
         type="textarea"
         autosize
        label="商品名称"
      />
      <view
        class="start_pick_container"
        @tap="choose_time"
        style="margin-bottom: 10rpx"
        v-if="from=='near'"
      >

        <van-field
          :value="value"
          :border="false"
          label="砍价倒计时时间"
          disabled
        />
        <view v-if="!countdown" class="placeholder">请选择砍价倒计时时间</view>
        <view v-if="bargin_ruleForm.hour" class="placeholder">{{
          countdown
        }}</view>
      </view>
      <van-field
        :value="bargin_ruleForm.need_times"
        placeholder="请输入次数"
        :border="false"
        @change="change_need_times"
        label="需要砍多少次"
        v-if="from=='twitter'"
      />
      <van-field
        :value="bargin_ruleForm.one_people_times"
        placeholder="请输入每人次数"
        :border="false"
        @change="change_per_times"
        label="每人次数(默认次数)"
        v-if="from=='twitter'"
      />
      <van-field
        :value="bargin_ruleForm.price"
        placeholder="请输入商品价格"
        :border="false"
        disabled
        label="商品价格"
      />
      <van-field
        :value="bargin_ruleForm.cut_price"
        placeholder="请输入保底价"
        :border="false"
        @change="change_cut_price"
        label="保底价"
         v-if="from=='twitter'"
      />
      <van-field
        :value="bargin_ruleForm.success_times"
        placeholder="请输入已成功次数"
        :border="false"
        @change="change_suc_times"
        label="成功次数"
        v-if="from=='twitter'"
      />
      <van-field
        :value="bargin_ruleForm.open_times"
        placeholder="请输入活动被开启次数"
        :border="false"
        @change="change_open_times"
        label="已开启次数"
        v-if="from=='twitter'"
      />
      <van-field
        :value="bargin_ruleForm.limit_radius_pub "
        placeholder="公里范围数内可参与此砍价"
        :border="false"
        @change="change_limit_radius"
        label="公里数/KM"
        v-if="from='near'"
      />
      <van-cell  v-if="from=='twitter'">
        <view slot="title">{{bargin_ruleForm.show_need_times==1?'显示砍价所需刀数':'不显示所需刀数'}}</view>
        <view slot="">
          <van-switch :checked="bargin_ruleForm.show_need_times==1?true:false" @change="change_show_need_times" />
        </view>
      </van-cell>
      <van-cell v-if="from=='twitter'" >
        <view slot="title">{{bargin_ruleForm.show_bj_price==1?'显示保底价':'不显示保底价'}}</view>
        <view slot="">
          <van-switch :checked="bargin_ruleForm.show_bj_price==1?true:false" @change="change_show_bj_price" />
        </view>
      </van-cell>
      <view style="padding-left: 30upx">图片(图片大小不超过1m)</view>
      <view class="upload" style="padding-left: 20upx">
        <uImg
          ref="upimg"
          :canUploadFile="true"
          :limit="limit"
          :uploadFileUrl="uploadFileUrl"
          :uImgList.sync="uImgList"
          @uploadSuccess="uploadSuccess"
          :fileKeyName="name"
        />
        <van-button
          type="primary"
          size="mini"
          style="margin-left: 20upx"
          @tap="upload"
          >确定上传</van-button
        >
      </view>
      <div class="btn_box">
        <van-button @tap="submit" size="large" color="#f95a4b">提交</van-button>
        <van-button @tap="cancle_submit" size="large">返回</van-button>
      </div>
    </view>

    <w-picker
      :visible="show_time_picker"
      mode="time"
      :value="bargin_ruleForm.hour"
      :second="false"
      @confirm="confirm_time1"
      @cancel="cancle_time1"
      ref="date1"
    ></w-picker>
    <van-toast id="van-toast" />
  </view>
</template>

<script>
import wPicker from "@/components/w-picker/w-picker.vue";
import ProductBox from "../components/productBox";
import { query_xp_goods,item_save } from "../../utils/imapi";
import uImg from "../../components/zhtx-uploadImg/zhtx-uploadImg";
export default {
  props: {},
  components: {
    ProductBox,
    uImg,
    wPicker,
  },
  data() {
    return {
      productList: [],
      queryparam1: {
        page_index: 1,
        page_size: 10,
        goods_status: "",
        goods_id: "",
      },
      rule_show: false,
      show: false,
      bargin_ruleForm: {
        item_id: "",
        p: "XP",
        status: 0,
        hour: "",
        minutes:'',
        need_times: "",
        one_people_times: 1,
        cut_item_title: "",
        pos_pic: "",
        price: 0,
        open_times: 0,
        success_times: 0,
        cut_price: "",
        cut_mode:'',
        show_need_times:0,
        show_bj_price:0,
        limit_radius_pub:'',
      },
      uImgList: [],
      value: "",
      uploadFileUrl:
        "https://api.comm.t.xingkonglian.net:8098/api/v1/oss/upload_starx",
      limit: 1,
      name: "file",
      show_time_picker: false,
      minHour: "00",
      maxHour: "23",
      countdown: "",
      from:''
    };
  },
  computed: {},
  watch: {},
  onLoad(options) {
    if(options.from) {
      this.from = options.from
      if(options.from=='twitter') {
        this.bargin_ruleForm.hour='2'
        this.bargin_ruleForm.minutes='0'
        this.bargin_ruleForm.cut_mode='2'
      }else if(options.from=='near') {
        this.bargin_ruleForm.cut_mode='3',
        this.bargin_ruleForm.need_times=20,
        this.bargin_ruleForm.show_need_times=1
        this.bargin_ruleForm.show_bj_price=1,
        this.bargin_ruleForm.cut_price=0
      }
      console.log(options.from);
    }
    this.query_xp_goods();
  },
  onReady() {},
  onShow() {},
  methods: {
    uploadSuccess(result) {
      if (result.statusCode == 200) {
        //上传成功的回调处理
        console.log(result.data);
        const img = JSON.parse(result.data).data
        console.log(img);
        this.bargin_ruleForm.pos_pic=img
        console.log(result);
      } else {
      }
    },
    async query_xp_goods() {
      const data = await query_xp_goods(this.queryParam1);
      console.log(data);
      this.productList = data.data;
    },
    upload() {
      this.$refs.upimg.upload();
    },
    onInput() {},
    confirm_time1(e) {
      console.log(e);
      this.countdown = e.result;
      this.bargin_ruleForm.hour = e.result.split(":")[0];
      this.bargin_ruleForm.minutes = e.result.split(":")[1];
      console.log(this.bargin_ruleForm);
      this.show_time_picker = false;
    },
    cancle_time1() {
      this.show_time_picker = false;
    },
    change_need_times(e) {
      this.bargin_ruleForm.need_times=e.detail
    },
    change_per_times(e) {
      this.bargin_ruleForm.one_people_times=e.detail
    },
    change_cut_title(e) {
      this.bargin_ruleForm.cut_item_title=e.detail
    },
    change_cut_price(e) {
      this.bargin_ruleForm.cut_price=e.detail
    },
    change_suc_times(e) {
      this.bargin_ruleForm.success_times=e.detail
    },
    change_open_times(e) {
      this.bargin_ruleForm.open_times=e.detail
    },
    change_limit_radius(e) {
      this.bargin_ruleForm.limit_radius_pub=e.detail
    },
    choose_time() {
      this.show_time_picker = true;
    },
    change_show_need_times(e) {
      console.log(e);
      this.bargin_ruleForm.show_need_times=e.detail?1:0
    },
    change_show_bj_price(e) {
      console.log(e);
      this.bargin_ruleForm.show_bj_price=e.detail?1:0
    },
   async submit() {
          console.log(this.bargin_ruleForm);
     if(this.countdown==''||(this.bargin_ruleForm.need_times==''&&this.bargin_ruleForm.need_times!=0)||this.bargin_ruleForm.one_people_times==''||this.bargin_ruleForm.cut_item_title==''||this.bargin_ruleForm.pos_pic==''||this.bargin_ruleForm.price==''||(this.bargin_ruleForm.open_times==''&&this.bargin_ruleForm.open_times!=0)||(this.bargin_ruleForm.success_times==''&&this.bargin_ruleForm.success_times!=0 )||(this.bargin_ruleForm.cut_price==''&&this.bargin_ruleForm.cut_price!=0)) {
       this.$toast('请填写完整数据')
       return
     }
     this.bargin_ruleForm.cut_price=Number(this.bargin_ruleForm.cut_price)
     this.bargin_ruleForm.hour=Number(this.bargin_ruleForm.hour)
     this.bargin_ruleForm.minutes=Number(this.bargin_ruleForm.minutes)
     this.bargin_ruleForm.need_times=Number(this.bargin_ruleForm.need_times)
     this.bargin_ruleForm.one_people_times=Number(this.bargin_ruleForm.one_people_times)
     this.bargin_ruleForm.open_times=Number(this.bargin_ruleForm.open_times)
     this.bargin_ruleForm.price=Number(this.bargin_ruleForm.price)
     this.bargin_ruleForm.success_times=Number(this.bargin_ruleForm.success_times)
     this.bargin_ruleForm.show_need_times=Number(this.bargin_ruleForm.show_need_times)
     this.bargin_ruleForm.show_bj_price=Number(this.bargin_ruleForm.show_bj_price)
     this.bargin_ruleForm.limit_radius_pub=Number(this.bargin_ruleForm.limit_radius_pub)
     if(this.from=='twitter') {
       this.bargin_ruleForm.limit_radius_pub=0
     }else {
       if(this.bargin_ruleForm.limit_radius_pub=='') {
         this.bargin_ruleForm.limit_radius_pub=0
       }else {
         this.bargin_ruleForm.limit_radius_pub=Number(this.bargin_ruleForm.limit_radius_pub)
       }
     }
      try {
        
      const data = await item_save(this.bargin_ruleForm)
      if(data.code==0) {
        this.$toast.success('添加成功')
        this.rule_show=false
      }

      }catch(err) {
        this.$toast.success('添加失败')
      }
       this.bargin_ruleForm={
          item_id: "",
        p: "XP",
        status: 1,
        hour: "2",
        minutes:'0',
        need_times: "",
        one_people_times: 1,
        cut_item_title: "",
        pos_pic: "",
        price: 0,
        open_times: 0,
        success_times: 0,
        cut_price: "",
        cut_mode:'2',
        show_need_times:0,
        show_bj_price:0,
        limit_radius_pub:0
       } 
      
    },
    cancle_submit() {
      console.log("111");
      this.rule_show = false;
    },
    choose_bargin(e) {
      console.log(e);
      this.bargin_ruleForm.item_id=e.goods_code
      this.bargin_ruleForm.pos_pic=e.cover_image
      this.bargin_ruleForm.price=e.price2
      this.bargin_ruleForm.cut_item_title=e.goods_title
      this.rule_show=true
    }
  },
  onReachBottom() {},
  onHide() {},
};
</script>

<style scoped lang="scss" scoped>
/deep/ .van-cell__title {
  width: 250upx;
  max-width: 250upx !important;
}
.count_down {
  display: flex;
}
.start_pick_container {
  display: flex;
  align-items: center;
  position: relative;
  .placeholder {
    position: absolute;
    top: 50%;
    left: 130px;
    transform: translateY(-50%);
    color: #333333;
  }
}
.upload {
  display: flex;
  align-items: center;
}
.btn_box {
  padding: 0 20px;
  margin-top: 20upx;
  /deep/ .van-button:first-child {
    margin-bottom: 20upx;
  }
}
.create_acativty {
  width: 100%;
  height: 96px;
  background: linear-gradient(90deg, #fdb482 0%, #f95a4b 100%);
  line-height: 96px;
  text-align: center;
  font-size: 36px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: #ffffff;
  margin-top: 40px;
}
</style>
